package com.war.controller.outofstorage;



import com.war.pojo.outofstorage.ProductOut;
import com.war.pojo.outofstorage.ProductReturn;
import com.war.pojo.system.Customer;
import com.war.pojo.system.Warehouse;
import com.war.service.outofstorage.ProductReturnService;
import com.war.service.safeManage.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/productReturn")
public class ProductReturnController {
    @Autowired(required = false)
    ProductReturnService productReturnService;
    //访问productOut-list页面
    @RequestMapping("/return")
    public String list(ProductReturn pr, ModelMap m){
        HashMap<String,Object> map= productReturnService.select(pr);
        m.put("info",map);

        m.put("preCondition",pr.getCondition());
        m.put("start",pr.getStartDate());
        m.put("end",pr.getEndDate());
        return "outOfStorage/productReturn-list";
    }
    //退货
    @RequestMapping("/addReturn")
    public String addReturn(){
        return "outOfStorage/productReturn-add";
    }

    @RequestMapping("/addReturnAjax")
    @ResponseBody
    public HashMap<String,Object> addReturnAjax(ProductReturn pr){
        HashMap<String,Object> map = new HashMap<>();

        String info=productReturnService.addReturn(pr);
        map.put("info",info);
        return map;
    }
    //打开详情页面
    @RequestMapping("getDetail")
    public String getDetail(ProductReturn pr,ModelMap m){
        ProductReturn reInfo=productReturnService.selectByReturnId(pr);
        m.addAttribute("reInfo",reInfo);
        return "outOfStorage/productReturn-detail";
    }

    //处理删除的ajax
    @RequestMapping("/delInfo")
    @ResponseBody
    public HashMap<String,Object> delInfo(ProductReturn pr){
        HashMap<String,Object> map=new HashMap<>();
        String result=productReturnService.delInfo(pr);
        map.put("info",result);
        return map;
    }

    //打开修改页面
    @RequestMapping("/update")
    public String update(ProductReturn pr,ModelMap m){
        ProductReturn reInfo=productReturnService.selectByReturnId(pr);
        m.addAttribute("reInfo",reInfo);
        return "outOfStorage/productReturn-edit";
    }
    //处理修改的ajax
    @RequestMapping("/updateAjax")
    @ResponseBody
    public HashMap<String,Object> updateAjax(ProductReturn pr){
        HashMap<String,Object> map=new HashMap<>();
        String result=productReturnService.update(pr);
        map.put("info",result);
        return map;
    }

    // 添加退货记录时的出库编号是否存在
    @RequestMapping("/existWarehousingIdAjax")
    @ResponseBody
    public HashMap<String,Object> existWarehousingIdAjax(ProductOut po){
        HashMap<String,Object> map = new HashMap<>();
        String result = productReturnService.ifExistWarehousingId(po);
        map.put("info",result);
        return map;
    }

    //处理比较退货数量与入库数量的ajax请求
    @RequestMapping("/storageNumOver")
    @ResponseBody
    public HashMap<String,Object> storageNumOver(ProductReturn pr){
        HashMap<String,Object> map = new HashMap<>();
        int result = productReturnService.storageNumOver(pr);
        map.put("info",result);
        return map;
    }

    //批量删除
    @RequestMapping(value = "delAll",method = RequestMethod.POST)
    @ResponseBody
    public HashMap<String,Object> delAll(@RequestBody List<ProductReturn> pr){
        HashMap<String,Object> map=new HashMap<String, Object>();
        String result=productReturnService.delAll(pr);
        map.put("info",result);
        return map;
    }

    //加载客户名字
    @RequestMapping("/clientName")
    @ResponseBody
    public List<Customer> selectClientName(){
        return productReturnService.selectClientName();
    }


    //加载仓库名称
    @RequestMapping("/warehouseName")
    @ResponseBody
    public List<Warehouse> selectWarehouseName(){
        return productReturnService.selectWarehouseName();
    }
    //获取仓库的管理员名称
    @RequestMapping("/getAdministrator")
    @ResponseBody
    public String getAdministrator(Warehouse warehouse){
        return productReturnService.getAdministrator(warehouse);
    }
}
